package com.KnowingIdea.ConcreteMixCalculation.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.ClickEvent;

public class uiCalculatorSelect extends Composite{

	private static uiCalculatorSelectUiBinder uiBinder = GWT.create(uiCalculatorSelectUiBinder.class);
	private final CalculatorSelectServiceAsync calculatorSelectService = GWT.create(CalculatorSelectService.class);
	
	@UiField Button bottonSelect;
	@UiField TextBox textPassCode;
	@UiField RadioButton radioRoad;
	@UiField RadioButton radioGravel;

	interface uiCalculatorSelectUiBinder extends UiBinder<Widget, uiCalculatorSelect> {
	}

	public uiCalculatorSelect() {
		initWidget(uiBinder.createAndBindUi(this));
	}
	
	@UiHandler("bottonSelect")
	void onBottonSelectClick(ClickEvent event) {
		if (textPassCode.getValue().equals("")) {
			Window.alert("许可码不能为空！");
		}
		else {
			calculatorSelectService.calculatorSelectService(textPassCode.getValue(), new AsyncCallback <String>() {

				@Override
				public void onFailure(Throwable caught) {
					
				}

				@Override
				public void onSuccess(String result) {
					if (result.equals("No")){
						Window.alert("许可码不正确！");
					}
					else {
						if (radioGravel.getValue().equals(true)) {
							((RootLayoutPanel) getParent()).add(new uiGravelMixCalculator());
							removeFromParent();
						}
						if (radioRoad.getValue().equals(true)) {
							((RootLayoutPanel) getParent()).add(new uiRoadMixCalculator());
							removeFromParent();
						}
					}
				}
			});
		}
	}
}
